//
//  LLEmojiTool.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/7/5.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

// 显示的列数
let EmoticonMaxCol = 7
// 显示的行数
let EmoticonMaxRow = 3
// 每页显示的个数
let EmoticonMaxCount = EmoticonMaxCol * EmoticonMaxRow - 1

class LLEmojiTool: NSObject {
    
    //单例
    static var sharedTool: LLEmojiTool = LLEmojiTool()
    
    //获取bundle文件
    lazy var emoticonbundle: NSBundle = {
        
        let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: bundlePath)!
    }()
    
    //default 表情 一维数组
    lazy var defaultemoticons: [LLEmoticonBundleModel] = {
        
       return  self.getEmoticons("default/")
        
    }()
    
    //emoji 表情 一维数组
    lazy var emojiemoticons: [LLEmoticonBundleModel] = {
        
       return  self.getEmoticons("emoji/")
    }()
    
    //lxh 表情 一维数组
    lazy var lxhemoticons: [LLEmoticonBundleModel] = {
        
       return  self.getEmoticons("lxh/")
    }()
    
    
    //表情 三维数组
    lazy var allemoticons: [[[LLEmoticonBundleModel]]] = {
        
       return  [self.getEmoticonGroup(self.defaultemoticons),
               self.getEmoticonGroup(self.emojiemoticons),
               self.getEmoticonGroup(self.lxhemoticons)]
        
        
    }()
    //读取boundle文件中plist文件中的数组转换为模型
    func getEmoticons(fileNmae: String) ->[LLEmoticonBundleModel]{
        
        let filePath = emoticonbundle.pathForResource("\(fileNmae)info.plist", ofType: nil)!
        
        let array = NSArray(contentsOfFile: filePath)!
        
        var tempArray: [LLEmoticonBundleModel] = [LLEmoticonBundleModel]()
        
        for dict in array {
            
            let e = LLEmoticonBundleModel(dict: dict as! [String: AnyObject])
            
            e.path = "\(fileNmae)"
            
            tempArray.append(e)
        }
        return tempArray
        
    }
    
    
    //一维数组转二维数组
    func getEmoticonGroup(emoticon: [LLEmoticonBundleModel])->[[LLEmoticonBundleModel]]{
        
        //计算页数
        let pageCount = (emoticon.count + EmoticonMaxCount - 1) / EmoticonMaxCount
        
        var tempArray: [[LLEmoticonBundleModel]] = [[LLEmoticonBundleModel]]()
        
        for i in 0..<pageCount{
            
            //位置和长度
            let loc = EmoticonMaxCount * i
            
            var len = EmoticonMaxCount
            
            if len + loc > emoticon.count{
                
                 len =  emoticon.count - loc
            }
            
            let range = NSMakeRange(loc , len)
            
            let arr = (emoticon as NSArray).subarrayWithRange(range) as! [LLEmoticonBundleModel]
            
            tempArray.append(arr)
        }
        
        return tempArray
    }
    
    func getSearchEmoticon(chs: String) -> LLEmoticonBundleModel? {
        
        // 遍历默认
        for e in defaultemoticons {
            // 如果chs相同
            if e.chs == chs{
                return e
            }
        }
        
        // 遍历浪小花
        for e in lxhemoticons {
            // 如果chs相同
            if e.chs == chs {
                return e
            }
        }
        
        return nil
    }

    

}
